#pragma once
// mysql版本
#include "../comm/log.hpp"
#include "../comm/util.hpp"
#include <iostream>
#include <string>
#include <assert.h>
#include <unordered_map>
#include <vector>
#include <mysql/mysql.h>

// 根据list文件，加载所有题目信息到内存中
// model：主要用来和数据进行交互，对外提供访问数据接口

namespace NS_model
{
    using namespace std;
    using namespace NS_log;
    using namespace NS_util;
    struct Question
    {
        string number; // 题目编号
        string title;  // 题目标题
        string star;   // 难度
        string desc;   // 题目的描述
        string header; // 题目预设编辑器代码
        string tail;   // 题目的测试用例
        int cpu_limit; // 时间要求
        int mem_limit; // 空间要求
    };
    const std::string oj_questions = "oj_questions";
    const std::string host = "127.0.0.1";
    const std::string user = "oj_client";
    const std::string passwd = "123456";
    const std::string db = "oj";
    const int port = 3306;

    class Model
    {
    public:
        Model()
        {
        }
        bool QueryMySql(const std::string &sql,vector<Question> *out)
        {
            //创建mysql句柄
            MYSQL *my = mysql_init(nullptr);

            //连接数据库
            if(nullptr == mysql_real_connect(my,host.c_str(),user.c_str(),passwd.c_str(),db.c_str(),port,nullptr,0))
            {
                LOG(FATAL) << "连接数据库失败" << "\n";
                return false;
            }
            LOG(INFO) << "连接数据库成功" << "\n";

            //执行数据中的sql语句
            if(0 != mysql_query(my,sql.c_str()))
            {
                LOG(WARRING) << sql << " execute error " << "\n";
                return false;
            }

            //提取结果
            MYSQL_RES *res = mysql_store_result(my);
            //分析结果
            int rows = mysql_num_rows(res);//行
            int cols = mysql_num_fields(res);//列
            Question q;
            for(int i = 0; i < rows; i++)
            {
                MYSQL_ROW row = mysql_fetch_row(res);
                 q.number = row[0];
                 q.title = row[1];
                 q.star = row[2];
                 q.desc = row[3];
                 q.header = row[4];
                 q.tail = row[5];
                 q.cpu_limit = stoi(row[6]);
                 q.mem_limit = stoi(row[7]);

                 out->push_back(q);
            }
            //释放结果空间
            mysql_free_result(res);
            mysql_close(my);
            return true;
        }
        bool GetAllQuestions(vector<Question> *out) // 获取全部题目
        {   
            std::string sql = "select * from ";
            sql += oj_questions;
            return QueryMySql(sql,out);
        }
        bool GetOneQuestion(const std::string &number, Question *q) // 获取一个题目
        {
            bool res = false;
            std::string sql = "select * from ";
            sql += oj_questions;
            sql += " where number=";
            sql += number;
            std::vector<Question> result;
            if(QueryMySql(sql,&result))
            {
                if(result.size() == 1)
                {
                    *q = result[0];
                    res = true;
                }
                
            }
            return res;
        }
        ~Model()
        {
        }
    };
}